package com.three.box.activity

import android.content.Intent
import android.os.Bundle
import android.text.Editable
import com.tencent.mmkv.MMKV
import com.three.box.MainActivity
import com.three.box.base.BaseActivity
import com.three.box.config.Constant
import com.three.box.databinding.ActivityLoginBinding
import com.three.box.network.AppRepository
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

class LoginActivity : BaseActivity() {

    companion object {
        private const val TAG = "LoginActivity"
    }

    private lateinit var binding: ActivityLoginBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        binding = ActivityLoginBinding.inflate(layoutInflater)
        setStatusBar()
        super.onCreate(savedInstanceState)
        val mmkv = MMKV.defaultMMKV()
        if (!mmkv.decodeString(Constant.TOKEN).isNullOrEmpty()) {
            gotoMain()
        }
//        gotoMain()
        initView()
    }

    override fun getLayoutView() = binding.root

    private fun initView() {
        if (Constant.TEST) {
            binding.accountEt.text = Editable.Factory.getInstance().newEditable("15102972846")
            binding.passwordEt.text = Editable.Factory.getInstance().newEditable("88886666")
        }
//        binding.accountEt.text = Editable.Factory.getInstance().newEditable("15102972846")
//        binding.passwordEt.text = Editable.Factory.getInstance().newEditable("88886666")
//        binding.accountEt.text = Editable.Factory.getInstance().newEditable("youke")
//        binding.passwordEt.text = Editable.Factory.getInstance().newEditable("88886666")
        binding.loginBtn.setOnClickListener {
            val account = binding.accountEt.text.toString()
            val password = binding.passwordEt.text.toString()

            CoroutineScope(Dispatchers.IO).launch {
                val success = AppRepository.instance.loginOrRegister(account, password)
                if (success) {
                    gotoMain()
                }
            }
        }
    }

    private fun gotoMain() {
        startActivity(Intent(this@LoginActivity, MainActivity::class.java))
        finish()
    }
}


















